package com.netctoss.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	private static ThreadLocal<Session> tl = new ThreadLocal<Session>();
	static{
		Configuration conf = 
			   new Configuration();
		conf.configure("/hibernate.cfg.xml");
		sf = conf.buildSessionFactory();
	}
	
	
	public static Session getSession(){
		Session session = tl.get();
		if(session == null || !session.isOpen()){
			session = sf.openSession();
			tl.set(session);
		}
		return session;
	}
	
	public static void closeSession(){
		Session session = tl.get();
		tl.set(null);
		if(session != null){
			session.close();
		}
	}
	public static void main(String[] args){
		Session session1 = getSession();
		Session session2 = getSession();
		System.out.println(session1==session2);
	}
}
